/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package netoppiggy_server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Nguyen Minh Huong
 */
public class NetOpPiggy_Server {

    public static final int LISTENING_PORT = 1102;
    private static ServerSocket welcomeSocket = null;
    private static Socket client = null;
    static DataOutputStream dos = null;
    static DataInputStream dis = null;
    static ChatWithClientsForm chatFrm = null;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //ServerMainForm frm = new ServerMainForm();
        
        //frm.setClientForm(chatFrm);       
        try {
            welcomeSocket = new ServerSocket(LISTENING_PORT);
            ChatWithClientsForm frm1 = new ChatWithClientsForm(welcomeSocket);
            frm1.setVisible(true);
            //frm.setServerSocket(welcomeSocket);
            int id = 0;
            while (true) {
                Socket client1 = welcomeSocket.accept();
                //ClientServiceThread cliThread = new ClientServiceThread(client1, id);
                //PrintWriter dos11 = new PrintWriter(new OutputStreamWriter(client1.getOutputStream()));
                //dos11.println("tran van dai");
                //dos11.flush();
                
                ClientServiceThread cliThread= new ClientServiceThread(client1, id);
                Thread t = new Thread(cliThread);
                t.start();
               // dos = new DataOutputStream(client1.getOutputStream());
                //dis = new DataInputStream(client1.getInputStream());
                
                
                
                //frm.addClientSocket(cliThread);

                /*if (frm.chatFrm == null) {
                    frm.chatFrm = new ChatWithClientsForm();
                    frm.chatFrm.setTitle("Chat with Clients Form");
                    frm.chatFrm.setVisible(true);
                }*/
                cliThread._frmChat = frm1;
                frm1.addNewChatTab1(client1, id);
                JOptionPane.showMessageDialog(null, "Client request accepted");
                
                id ++;
            }



        } catch (IOException ex) {
            Logger.getLogger(ServerMainForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
